<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类ID" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入分类ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-input
          v-model="queryParams.cover"
          placeholder="请输入封面"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态，1-在售，0-下架" prop="isShow">
        <el-input
          v-model="queryParams.isShow"
          placeholder="请输入商品状态，1-在售，0-下架"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签，如“超值热销”、“精选好货”等" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入标签，如“超值热销”、“精选好货”等"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用于排序的分数" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入用于排序的分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品评分" prop="rating">
        <el-input
          v-model="queryParams.rating"
          placeholder="请输入商品评分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价数量" prop="reviewCount">
        <el-input
          v-model="queryParams.reviewCount"
          placeholder="请输入评价数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="逻辑删除标记，0-未删除，1-已删除" prop="isDeleted">
        <el-input
          v-model="queryParams.isDeleted"
          placeholder="请输入逻辑删除标记，0-未删除，1-已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['front:products:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['front:products:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['front:products:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['front:products:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID，主键，自增" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="商品描述" align="center" prop="description" />
      <el-table-column label="分类ID" align="center" prop="categoryId" />
      <el-table-column label="封面" align="center" prop="cover" />
      <el-table-column label="商品图片URL" align="center" prop="imageUrl" />
      <el-table-column label="商品状态，1-在售，0-下架" align="center" prop="isShow" />
      <el-table-column label="标签，如“超值热销”、“精选好货”等" align="center" prop="tags" />
      <el-table-column label="用于排序的分数" align="center" prop="score" />
      <el-table-column label="商品评分" align="center" prop="rating" />
      <el-table-column label="评价数量" align="center" prop="reviewCount" />
      <el-table-column label="逻辑删除标记，0-未删除，1-已删除" align="center" prop="isDeleted" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['front:products:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['front:products:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分类ID" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类ID" />
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <el-input v-model="form.cover" placeholder="请输入封面" />
        </el-form-item>
        <el-form-item label="商品图片URL" prop="imageUrl">
          <el-input v-model="form.imageUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品状态，1-在售，0-下架" prop="isShow">
          <el-input v-model="form.isShow" placeholder="请输入商品状态，1-在售，0-下架" />
        </el-form-item>
        <el-form-item label="标签，如“超值热销”、“精选好货”等" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入标签，如“超值热销”、“精选好货”等" />
        </el-form-item>
        <el-form-item label="用于排序的分数" prop="score">
          <el-input v-model="form.score" placeholder="请输入用于排序的分数" />
        </el-form-item>
        <el-form-item label="商品评分" prop="rating">
          <el-input v-model="form.rating" placeholder="请输入商品评分" />
        </el-form-item>
        <el-form-item label="评价数量" prop="reviewCount">
          <el-input v-model="form.reviewCount" placeholder="请输入评价数量" />
        </el-form-item>
        <el-form-item label="逻辑删除标记，0-未删除，1-已删除" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入逻辑删除标记，0-未删除，1-已删除" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">商户商品库存信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddMerchantProductInventory">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMerchantProductInventory">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="merchantProductInventoryList" :row-class-name="rowMerchantProductInventoryIndex" @selection-change="handleMerchantProductInventorySelectionChange" ref="merchantProductInventory">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="商户ID" prop="merchantId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.merchantId" placeholder="请输入商户ID" />
            </template>
          </el-table-column>
          <el-table-column label="库存数量" prop="stock" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.stock" placeholder="请输入库存数量" />
            </template>
          </el-table-column>
          <el-table-column label="销量" prop="sales" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sales" placeholder="请输入销量" />
            </template>
          </el-table-column>
          <el-table-column label="折扣" prop="discount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.discount" placeholder="请输入折扣" />
            </template>
          </el-table-column>
          <el-table-column label="逻辑删除标记，0-未删除，1-已删除" prop="isDeleted" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isDeleted" placeholder="请输入逻辑删除标记，0-未删除，1-已删除" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createdAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createdAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="updatedAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.updatedAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择更新时间" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProducts, getProducts, delProducts, addProducts, updateProducts } from "@/api/admin/products";

export default {
  name: "Products",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMerchantProductInventory: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productsList: [],
      // 商户商品库存表格数据
      merchantProductInventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        description: null,
        categoryId: null,
        cover: null,
        imageUrl: null,
        isShow: null,
        tags: null,
        score: null,
        rating: null,
        reviewCount: null,
        isDeleted: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ],
        cover: [
          { required: true, message: "封面不能为空", trigger: "blur" }
        ],
        imageUrl: [
          { required: true, message: "商品图片URL不能为空", trigger: "blur" }
        ],
        isShow: [
          { required: true, message: "商品状态，1-在售，0-下架不能为空", trigger: "blur" }
        ],
        isDeleted: [
          { required: true, message: "逻辑删除标记，0-未删除，1-已删除不能为空", trigger: "blur" }
        ],
        createdAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatedAt: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listProducts(this.queryParams).then(response => {
        this.productsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        description: null,
        categoryId: null,
        cover: null,
        imageUrl: null,
        isShow: null,
        tags: null,
        score: null,
        rating: null,
        reviewCount: null,
        isDeleted: null,
        createdAt: null,
        updatedAt: null
      };
      this.merchantProductInventoryList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProducts(id).then(response => {
        this.form = response.data;
        this.merchantProductInventoryList = response.data.merchantProductInventoryList;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.merchantProductInventoryList = this.merchantProductInventoryList;
          if (this.form.id != null) {
            updateProducts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProducts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function() {
        return delProducts(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 商户商品库存序号 */
    rowMerchantProductInventoryIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 商户商品库存添加按钮操作 */
    handleAddMerchantProductInventory() {
      let obj = {};
      obj.merchantId = "";
      obj.stock = "";
      obj.sales = "";
      obj.discount = "";
      obj.isDeleted = "";
      obj.createdAt = "";
      obj.updatedAt = "";
      this.merchantProductInventoryList.push(obj);
    },
    /** 商户商品库存删除按钮操作 */
    handleDeleteMerchantProductInventory() {
      if (this.checkedMerchantProductInventory.length == 0) {
        this.$modal.msgError("请先选择要删除的商户商品库存数据");
      } else {
        const merchantProductInventoryList = this.merchantProductInventoryList;
        const checkedMerchantProductInventory = this.checkedMerchantProductInventory;
        this.merchantProductInventoryList = merchantProductInventoryList.filter(function(item) {
          return checkedMerchantProductInventory.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMerchantProductInventorySelectionChange(selection) {
      this.checkedMerchantProductInventory = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('front/products/export', {
        ...this.queryParams
      }, `products_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
